package com.situ.day11;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {

    @Test
    public void test1() {
        //运行时异常：运行的时候抛出的异常，是程序员自己写的代码有问题
        //java.lang.ArithmeticException: / by zero
        //int num = 1 / 0;

        // 编译时异常：未雨绸缪
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            System.out.println("ExceptionDemo.test1 try");
        } catch (FileNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }

        System.out.println("ExceptionDemo.test1");
    }

    @Test
    public void test2() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        try {
            show();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void show() throws ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
    }

    @Test
    public void test45() {
//        while (true) {
//
//        }
//        System.out.println("ExceptionDemo.test45");

        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }

        //FileNotFoundException extends IOException
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        } /*catch (FileNotFoundException e) {
            //Exception 'java.io.FileNotFoundException' has already been caught
            e.printStackTrace();
        }*/ finally {
            System.out.println("finally");
        }
    }

    @Test
    public void test22() {
        try {
            quQian(1900);
        } catch (MeiQianException e) {
            e.printStackTrace();
        }
    }

    public void quQian(double money) throws MeiQianException {
        if (money >= 1000) {
            throw new MeiQianException("钱不够");
        }
        System.out.println("钱够了");
    }


}
